/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import control.LogindHandler;
import control.Main;
import control.TextRegler;
import javax.swing.JLabel;

/**
 *
 * @author Morten J, Morten B & Jonas
 */
public class BrugerLogin extends javax.swing.JPanel {

    private LogindHandler lih;

    /**
     * Creates new form BrugerLogin
     */
    public BrugerLogin() {
        lih = new LogindHandler();
        initComponents();
    }

    public void userLogin() {
        try {
            lih.validateLogin(Integer.parseInt(jUser.getText()), jPass.getPassword());
        } catch (NumberFormatException ex) {
            GUI.getGuiHandler().setFeedbackMessage("Bruger navn må kun indeholde hele tal ", Main.getRED_COLOR());
        }
    }

    public JLabel getLabel() {
        return jLoginError;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jUserLabel = new javax.swing.JLabel();
        jUser = new javax.swing.JTextField();
        jPassLabel = new javax.swing.JLabel();
        jPass = new javax.swing.JPasswordField();
        jLoginBtn = new javax.swing.JButton();
        jLoginError = new javax.swing.JLabel();

        setBackground(new java.awt.Color(186, 153, 108));

        jUserLabel.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        jUserLabel.setForeground(new java.awt.Color(250, 250, 250));
        jUserLabel.setText("Brugernavn:");

        jUser.setBackground(java.awt.Color.darkGray);
        jUser.setFont(new java.awt.Font("Verdana", 1, 36)); // NOI18N
        jUser.setForeground(new java.awt.Color(250, 250, 250));
        jUser.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        jUser.setText("1");
        jUser.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(250, 250, 250)));
        jUser.setCaretColor(new java.awt.Color(250, 250, 250));
        jUser.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jUserActionPerformed(evt);
            }
        });
        jUser.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jUserKeyReleased(evt);
            }
        });

        jPassLabel.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        jPassLabel.setForeground(new java.awt.Color(250, 250, 250));
        jPassLabel.setText("Kodeord:");

        jPass.setBackground(java.awt.Color.darkGray);
        jPass.setFont(new java.awt.Font("Verdana", 1, 36)); // NOI18N
        jPass.setForeground(new java.awt.Color(250, 250, 250));
        jPass.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        jPass.setText("1234");
        jPass.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(250, 250, 250)));
        jPass.setCaretColor(new java.awt.Color(250, 250, 250));
        jPass.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jPassActionPerformed(evt);
            }
        });

        jLoginBtn.setBackground(java.awt.Color.darkGray);
        jLoginBtn.setFont(new java.awt.Font("Verdana", 1, 14)); // NOI18N
        jLoginBtn.setForeground(new java.awt.Color(250, 250, 250));
        jLoginBtn.setText("Log ind");
        jLoginBtn.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(250, 250, 250)));
        jLoginBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jLoginBtnActionPerformed(evt);
            }
        });

        jLoginError.setForeground(new java.awt.Color(222, 46, 46));
        jLoginError.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLoginBtn, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jUser, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPass, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jPassLabel)
                            .addComponent(jUserLabel))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(jLoginError, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(48, 48, 48)
                .addComponent(jUserLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jUser, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPassLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPass, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLoginError, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLoginBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(87, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jPassActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jPassActionPerformed
        userLogin();
    }//GEN-LAST:event_jPassActionPerformed

    private void jUserActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jUserActionPerformed
        userLogin();
    }//GEN-LAST:event_jUserActionPerformed

    private void jUserKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jUserKeyReleased
        TextRegler.onlyDig(jUser);
        if (jUser.getText().equals("")) {
            jLoginBtn.setEnabled(false);
        } else {
            jLoginBtn.setEnabled(true);
        }
    }//GEN-LAST:event_jUserKeyReleased

    private void jLoginBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jLoginBtnActionPerformed
        userLogin();
    }//GEN-LAST:event_jLoginBtnActionPerformed

    public void setFeedbackMessage(String feedbackMessage) {
        jLoginError.setText(feedbackMessage);
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jLoginBtn;
    private javax.swing.JLabel jLoginError;
    private javax.swing.JPasswordField jPass;
    private javax.swing.JLabel jPassLabel;
    private javax.swing.JTextField jUser;
    private javax.swing.JLabel jUserLabel;
    // End of variables declaration//GEN-END:variables
}
